﻿using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectIndieFarm
{
    public class PowerUpSystem : AbstractSystem, IPowerUpSystem, IArchive
    {
        public List<IPowerUp> PowerUps { get; } = new();

        public static BindableProperty<bool> PowerUpHadUnlockedToday = new();

        #region 存档相关
        /// <summary>
        /// 保存强化数据
        /// </summary>
        public void SaveData()
        {
            foreach (var powerup in PowerUps)
            {
                PlayerPrefs.SetInt($"power_up_{powerup.Key}_unlocked", powerup.Unlocked ? 1 : 0);
            }
        }
        /// <summary>
        /// 加载强化数据
        /// </summary>
        public void LoadData()
        {
            foreach (var powerup in PowerUps)
            {
                powerup.Unlocked = PlayerPrefs.GetInt($"power_up_{powerup.Key}_unlocked", 0) == 1;
            }
        }
        /// <summary>
        /// 重置强化数据
        /// </summary>
        public void ResetData()
        {
            foreach (var powerup in PowerUps)
            {
                powerup.Unlocked = false;
            }
        }
        #endregion


        protected override void OnInit()
        {
            PowerUpGroupSoilGrid.Setup(this);
            PowerUpGroupPlantAction.Setup(this);
            PowerUpGroupRipeCount.Setup(this);
            PowerUpGroupRipePercent.Setup(this);
        }

        public PowerUp Add(PowerUp powerUp)
        {
            PowerUps.Add(powerUp);
            return powerUp;
        }
    }
}
